//
//  MORFamilyMemberViewController.swift
//  aj
//
//  Created by leeqb on 2/17/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class MORFamilyMemberViewController: AJBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    var userList: [NSDictionary]?
    
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.tabBarController?.tabBar.hidden = true
        self.requestData()
    }
    
    private func requestData() {
        let params = ["userId": userId, "currentPage": 0, "pageSize": 10]
        CBMessage.shareInstance().showLoading()
        AJNetworkHelper.instance.post("aj_more_family_list", parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            self.userList = responseObject["result"]!!["userList"] as? [NSDictionary]
            self.collectionView.reloadData()
            }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
            }) { () -> Void in
                CBMessage.shareInstance().dismiss()
        }
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return userList?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MemberCell", forIndexPath: indexPath)
        
        let imageView = cell.viewWithTag(1) as! UIImageView
        let textLabel = cell.viewWithTag(2) as! UILabel
        
        let data = userList![indexPath.row]
        imageView.sd_setImageWithURL(NSURL(string: data["photo"] as? String ?? ""), placeholderImage: g_defaultImage)
        textLabel.text = data["callName"] as? String
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
    }

}
